package com.github.aloxc.plugin.restplus.component;

import javax.swing.table.AbstractTableModel;
import java.util.List;
import java.util.Vector;

/**
 * 变量表格数据
 * @author leero
 */
public class VariateTableModel extends AbstractTableModel {
    private String[] headers;
    protected Vector<Vector>    dataVector;
    public VariateTableModel(String[] headers, Vector<Vector> dataVector) {
        this.headers = headers;
        this.dataVector = dataVector;
    }

    public Vector<Vector> getDataVector(){
        return dataVector;
    }

    public void addRow(Vector rowData) {
        insertRow(getRowCount(), rowData);
    }
    public void addRows(Vector<Vector> rowsData) {
        dataVector.addAll(rowsData);
        fireTableDataChanged();
    }
    public void insertRow(int row, Vector rowData) {
        dataVector.insertElementAt(rowData, row);
        justifyRows(row, row+1);
        fireTableRowsInserted(row, row);
    }
    public void removeRow(int row) {
        dataVector.removeElementAt(row);
        fireTableRowsDeleted(row, row);
    }
    public void removeAll() {
        dataVector.clear();
        fireTableDataChanged();
    }

    public void removeRows(List<Integer> rowIndexList) {
        Vector<Vector> newDataList = new Vector<>();
        for (int i = 0; i < dataVector.size(); i++) {
            if(!rowIndexList.contains(i)){
                newDataList.add(dataVector.elementAt(i));
            }
        }
        dataVector.clear();
        dataVector.addAll(newDataList);
        fireTableDataChanged();;
//        fireTableRowsDeleted(row, row);
    }
    private void justifyRows(int from, int to) {
        // Sometimes the DefaultTableModel is subclassed
        // instead of the AbstractTableModel by mistake.
        // Set the number of rows for the case when getRowCount
        // is overridden.
        dataVector.setSize(getRowCount());

        for (int i = from; i < to; i++) {
            if (dataVector.elementAt(i) == null) {
                dataVector.setElementAt(new Vector(), i);
            }
            ((Vector)dataVector.elementAt(i)).setSize(getColumnCount());
        }
    }
    @Override
    public String getColumnName(int col) {
        return headers[col];
    }


    @Override
    public int getRowCount() {
        return this.dataVector.size();
    }

    @Override
    public int getColumnCount() {
        return this.headers.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Vector rowVector = (Vector)dataVector.elementAt(rowIndex);
        if(columnIndex==1){
            if("".equals(rowVector.elementAt(columnIndex))){
                return "----";
            }
        }
        return rowVector.elementAt(columnIndex);
    }

    @Override
    public Class getColumnClass(int columnIndex) {
        return getValueAt(0, columnIndex).getClass();
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        if (col < 2) {
            return true;
        } else {
            return true;
        }
    }

    @Override
    public void setValueAt(Object value, int row, int col) {
        Vector rowVector = (Vector)dataVector.elementAt(row);
        rowVector.setElementAt(value, col);
        fireTableCellUpdated(row, col);
    }
}
